package com.fingard.dsp.bank.directbank.ynht01;

import javax.xml.crypto.Data;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * Created by F on 2019/3/12.
 */
public class YNHT01Balance extends YNHT01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            while (p_req7506.readRecord()) {

                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                YNHT01Header header = new YNHT01Header();
                header.tranCode ="500001";
                Date date = new Date();
                header.termDate = Format.Date8Format.format(date);
                header.termTime = Format.Time6Format.format(date);
                header.seqNo = p_req7506.reqHeader.transSeqID;
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("ReqParam");
                xmlTextWriter.writeElementString("AcctNo",p_req7506.readedDetail.actNum);
                xmlTextWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspCode");
                        String returnMsg = tmpBkReader.getFirstText("/HTBankData/RspData/RspHead/RspMsg");

                        if ("000000".equals(returnCode)) {
                            if (tmpBkReader.read("/HTBankData/RspData/Result")){
                                detail.respCode = "0";
                                detail.respInfo = returnMsg;
                                detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                                detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("AcctNo");
                                detail.custName = tmpBkReader.readedXElement.getSingleText("CustName");
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBal");
                                detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Balance");

                            }
                        } else {
                            detail = new RetAtsBalDetail();
                            detail.respCode = "-1";
                            detail.respInfo = returnMsg;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
